﻿using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using ElectronicWMSA.Comm.ClassFiles;
using WMSABL;
using SYDZ.WMS.DBUtility;
using WMSAML.Warehouse.BLL;
using ElectronicWMSA.Comm.Forms.Message;
using System.Threading;

namespace ElectronicWMSA.Warehouse
{
    public partial class List_StockRuGoingFrm : Form
    {
        #region
        private string strWhere1 = "";
        PublicDataSet bll_PublicDataSet = new PublicDataSet();
        public List_StockRuGoingFrm()
        {
            InitializeComponent();
        }
        private void List_StockRuGoingFrm_Load(object sender, EventArgs e)
        {
            pageControl2.PageCurrent = 1; pageControl2.PageSize = 50;
        }
        #endregion

        #region
        public void DBindCombox()
        {
            if (true)
            {
                DataSet ds = new DataSet();
                ds = bll_PublicDataSet.GetDataSet("PD_Warehouse", "");
                DataTable dt = ds.Tables[0];
                DataRow dr = dt.NewRow();
                dr["NID"] = "0";
                dr["WarehouseName"] = "--请选择--";
                dt.Rows.InsertAt(dr, 0);

            }
        }
        #endregion

        #region 数据
        private int DBindData1()
        {
            ElectronicWMSA.Comm.Forms.MsControls.PageData pageData = new ElectronicWMSA.Comm.Forms.MsControls.PageData();
            pageData.TableName = "T_StockPrepare";
            pageData.PrimaryKey = "NID";
            pageData.OrderStr = "NID";
            pageData.PageIndex = this.pageControl2.PageCurrent;
            pageData.PageSize = this.pageControl2.PageSize;
            pageData.QueryCondition = strWhere1;
            pageData.QueryFieldName = "*";
            WaitingBoxFrm wf = new WaitingBoxFrm((obj, args) => { Thread.Sleep(500); pageData.QueryDataTable(); }, 500); wf.ShowDialog(this);
            pageControl2.bindingSource.DataSource = pageData.dsInfo.Tables[0];
            pageControl2.bindingNavigator.BindingSource = pageControl2.bindingSource;
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = this.pageControl2.bindingSource;
            dataGridView2.ClearSelection();
            dataGridView2.CurrentCell = null;
            return pageData.TotalCount;
        }
        private int pageControl1_EventPaging(ElectronicWMSA.Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindData1();
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        #endregion

        private void btnUserSearch_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("查询")) return;
            strWhere1 = " 1=1 AND PutList_NO  IS NOT NULL ";
            if (!string.IsNullOrEmpty(txt_PO.Text))
            {
                this.strWhere1 += "  AND  PutList_NO like '%" + txt_PO.Text.Trim() + "%'";
            }

            if (this.txt_Leixing.Text.Trim() != "--请选择--" && this.txt_Leixing.Text.Trim() != "")
                this.strWhere1 += " AND bk03 like '%" + this.txt_Leixing.Text.Trim() + "%' ";

            if (this.txt_Machine.Text.Trim() != "--请选择--" && this.txt_Machine.Text.Trim() != "")
                this.strWhere1 += " AND Product_Model  like  '%" + this.txt_Machine.Text.Trim() + "' ";
            if (!string.IsNullOrEmpty(txt_SN.Text))
            {
                this.strWhere1 += "  AND  Serial_Number like '" + txt_SN.Text.Trim() + "%'";
            }
            if (dtpTimeStart.Checked)
            {
                this.strWhere1 += "  AND  convert(varchar(10),Update_Time,120) >= '" + dtpTimeStart.Text.Trim() + "'";

            }
            if (dtpTimeEnd.Checked)
            {
                this.strWhere1 += "  AND  convert(varchar(10),Update_Time,120) <= '" + dtpTimeEnd.Text.Trim() + "'";
            }
            this.pageControl2.Bind();
        }

        #region  Close
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        #endregion

        private void btn_SAP_Click(object sender, EventArgs e)
        {
            DataTable dt = StrDBind(strWhere1);
            ExPut.GetNopiExcel(dt);
        }

        /// <summary>
        /// 查询datatable
        /// </summary>
        /// <param name="Where"></param>
        private DataTable StrDBind(string Where)
        {
            DataTable dt = new DataTable();
            string SelectSQL = "select [PutList_NO] as '入库单号',[BK03] as '入库类型',[Posiont1] as '库位',[Product_Model] as '机型',[Serial_Number] as '序列号',[Update_Time] as '入库时间',[BK06] as '入库数量' ,[BK01] as '入库设备' ,[Update_UserID] as '入库人ID'  from [T_StockPrepare]  ";
            if (Where != "")
            {
                SelectSQL += " where " + Where + "  ";
            }
            dt = DbHelperSQL.ExecuteDataTable(SelectSQL);
            return dt;
        }
        T_StockPrepare TBSbll = new T_StockPrepare();
        private void button1_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            int count = 0;
            string IDnum = "";
            for (int i = 0; i < dataGridView2.RowCount; i++)
            {
                if (dataGridView2.Rows[i].Cells[3].FormattedValue.ToString() == "True")//循环添加用户角色
                {
                    IDnum += "'" + dataGridView2.Rows[i].Cells["NID2"].Value.ToString() + "',";
                    count++;
                }
            }

            if (!string.IsNullOrEmpty(IDnum))
            {
                if (MessageBox.Show("确定撤销这" + count + "条记录吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DataTable dt = TBSbll.GetList("  NID in (" + IDnum.TrimEnd(',') + ")").Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        list.Add(" delete  T_StockPrepare where  Product_Model='" + dr["Product_Model"].ToString() + "' and  Serial_Number='" + dr["Serial_Number"].ToString() + "' and PutList_NO ='" + dr["PutList_NO"].ToString() + "'");
                        list.Add(" delete  T_Stock where  Product_Model='" + dr["Product_Model"].ToString() + "' and  Serial_Number='" + dr["Serial_Number"].ToString() + "' and PutList_NO ='" + dr["PutList_NO"].ToString() + "'");
                        StringBuilder sb = new StringBuilder();
                        sb.Append(" Insert into  [dbo].[T_GCStock]( ");
                        sb.Append("   [PutList_NO],[List_NOType],[Posiont1],[Product_Model] ");
                        sb.Append("  ,[Serial_Number],[Update_Time],[Update_UserID],Remark,[BK01],[BK03], BK06 ");
                        sb.Append(") values(");
                        sb.Append("'" + dr["PutList_NO"].ToString() + "','" + dr["List_NOType"].ToString() + "',");
                        sb.Append("'" + dr["Posiont1"].ToString() + "','" + dr["Product_Model"].ToString() + "',");
                        sb.Append("'" + dr["Serial_Number"].ToString() + "','" + dr["Update_Time"].ToString() + "',");
                        sb.Append("'" + SystemVariable.Login_Name + "','" + DateTime.Now.ToString() + "',");
                        sb.Append("'" + dr["BK01"].ToString() + "', '入库撤消','" + dr["Bk06"].ToString() + "'");
                        sb.Append("  )");
                        list.Add(sb.ToString());

                    }
                    if (DbHelperSQL.ExecuteSqlTran(list) == 0)
                    {
                        MessageBox.Show("操作失败");
                    }
                    else
                    {

                        MessageBox.Show("操作成功");
                        SystemVariable.AddLogInfo("入库撤消", "操作员：【" + SystemVariable.Login_Name + "】操作了撤消入库操作");
                        this.pageControl1.Bind();
                    }
                }
            }
            else
                MessageBox.Show("请选中行！");
        }

        private void CB_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                if (CB.Checked == true)
                {
                    dataGridView2.Rows[i].Cells[3].Value = "True";
                }
                else
                {
                    dataGridView2.Rows[i].Cells[3].Value = "False";
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = StrDBind1("");
            ExPut.GetNopiExcel(dt);
        }
        /// <summary>
        /// 查询datatable
        /// </summary>
        /// <param name="Where"></param>
        private DataTable StrDBind1(string Where)
        {
            DataTable dt = new DataTable();
            string SelectSQL = "select [PutList_NO] as '入库单号',[BK03] as '入库类型',[Posiont1] as '库位',[Product_Model] as '机型',[Serial_Number] as '序列号',[Update_Time] as '入库时间',[BK06] as '入库数量' ,[BK01] as '入库设备' ,[Update_UserID] as '入库人ID'  from [T_TemRuStock]  ";
            if (Where != "")
            {
                SelectSQL += " where " + Where + "  ";
            }


            dt = DbHelperSQL.ExecuteDataTable(SelectSQL);
            return dt;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Afrm frm = new Afrm();
            frm.ShowDialog();
        }


    }
}
